class Solution {
public:
    int bitsum(int n)
    {
        int sum = 0;
        while(n)
        {
            int t = n%10;
            sum+=t*t;
            n/=10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int fast = n;
        int slow = n;
        fast  = bitsum(fast);
        while(slow!=fast)
        {
            slow = bitsum(slow);
            fast = bitsum(bitsum(fast));
        }
        return slow == 1;
    }
};
